<?php

namespace App\V3\V3_2_6\Services;

use App\V3\V3_2_6\Models\ModuleModel;

class ModuleService
{
    //权限信息列表
    static public function roleModuleList()
    {
        $firstModuleInfo = ModuleModel::select(
            'id',
            'module_name',
            'upper_module_id',
            'module_img',
            'type',
            'sort',
        )
            ->where('upper_module_id', 0)
            ->where('delete_flag', 0)
            ->orderBy('sort', 'asc')
            ->orderBy('id', 'DESC')
            ->get()->toArray();

        $firstModuleId = [];
        foreach($firstModuleInfo as $k => $v) {
            $firstModuleId[] = $v['id'];
        }

        $secondModuleInfo = ModuleModel::select(
            'id',
            'module_name',
            'upper_module_id',
            'module_img',
            'type',
            'sort',
        )
            ->whereIn('upper_module_id', $firstModuleId)
            ->where('delete_flag', 0)
            ->orderBy('sort', 'asc')
            ->orderBy('id', 'DESC')
            ->get()->toArray();

        foreach($firstModuleInfo as $key => $value) {
            foreach($secondModuleInfo as $k => $v) {
                if($value['id'] == $v['upper_module_id']) {
                    $firstModuleInfo[$key]['second_module'][] = $v;
                }
            }
        }

        return $firstModuleInfo;
    }

    //权限列表
    static public function moduleList($data)
    {
        $result = ModuleModel::select(
            'id',
            'module_name',
            'module_img',
            'sort',
            'type',
            'icon',
            'url1',
            'url2',
            'platform'
        )
            ->with('secondModule', function($query) {
                $query->select(
                    'id',
                    'module_name',
                    'module_img',
                    'sort',
                    'type',
                    'upper_module_id',
                    'icon',
                    'url1',
                    'url2',
                    'platform'
                )
                    ->orderBy('sort', 'asc')
                    ->orderBy('id', 'desc')
                    ->where('delete_flag', 0);
            })
            ->where('delete_flag', 0)
            ->where('upper_module_id', 0)
            ->orderBy('sort', 'asc')
            ->orderBy('id', 'desc')
            ->paginate($data['pageSize'],['*'], '',$data['currentPage']);

        foreach($result as &$value) {
            if (!empty($value['platform'])) {
                $value['platform'] = array_filter(explode(',', $value['platform']), function ($v) {
                    return $v !== '';
                });
            } else {
                $value['platform'] = [];
            }

            if (!empty($value['secondModule'])) {
                foreach ($value['secondModule'] as &$val) {
                    if (!empty($val['platform'])) {
                        $val['platform'] = array_filter(explode(',', $val['platform']), function ($v) {
                            return $v !== '';
                        });
                    } else {
                        $val['platform'] = [];
                    }
                }
            }
        }
        return $result;
    }

    //新建模块权限
    static public function add($data)
    {
        if($data['upper_module_id'] != 0) {
            $condition[] = ['upper_module_id', '!=', 0];
        }
        else {
            $condition['upper_module_id'] = ['EQ' => 0];
        }

        $checkModuleInfo = ModuleModel::where([
            'module_name' => $data['module_name'],
            'delete_flag' => 0
        ])
            ->where($condition)
            ->first();

        if(!empty($checkModuleInfo)) {
            return ['code' =>1, 'msg' => '模块已存在', 'data' => ''];
        }

        $data['created_at'] = time();

        $insertId = ModuleModel::insertGetId($data);

        return ['code' => 0, 'msg' => 'success', 'data' => ['id' => $insertId]];
    }

    //修改
    static public function edit($data)
    {
        if($data['is_parent']) {
            $checkModuleInfo = ModuleModel::where([
                'module_name' => $data['module_name'],
                'delete_flag' => 0
            ])
                ->where('id', '!=', $data['id'])
                ->first();
        }
        else {
            if(empty($data['upper_module_id'])) {
                return ['code' => 1, 'msg' => '`upper_module_id`不能为空', 'data' => ''];
            }

            $checkModuleInfo = ModuleModel::where([
                'module_name' => $data['module_name'],
                'delete_flag' => 0,
                'upper_module_id' => $data['upper_module_id'],
            ])
                ->where('id', '!=', $data['id'])
                ->first();
        }

        if(!empty($checkModuleInfo)) {
            return ['code' =>1, 'msg' => '模块已存在', 'data' => ''];
        }

        unset($data['is_parent']);

        ModuleModel::where('id', $data['id'])->update($data);
        return ['code' => 0, 'msg' => 'success', 'data' => ''];
    }
}
